﻿using UnityEngine;
using System.Collections;

public partial class TankCitizenFSM : EnemyFSM
{
    void Start()
    {
        StartCoroutine(InitState());
    }

    IEnumerator InitState()
    {
        gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;


        yield return null;

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }


    void SetState(BASEUNIT_STATE iStateKey)
    {

        switch (iStateKey)
        {
            case BASEUNIT_STATE.IDLE:
                StartCoroutine(IdleState());
                break;
            case BASEUNIT_STATE.MOVE:
                StartCoroutine(MoveState());
                break;
            case BASEUNIT_STATE.CLOSEATTACK:
                StartCoroutine(CloseAtkState());
                break;
            case BASEUNIT_STATE.TRACE:
                StartCoroutine(TraceState());
                break;
            case BASEUNIT_STATE.DAMAGE:
                StartCoroutine(DamageState());
                break;
            case BASEUNIT_STATE.THROW:
                StartCoroutine(ThrowState());
                break;
            case BASEUNIT_STATE.DEAD:
                StartCoroutine(DeadState());
                break;
            case BASEUNIT_STATE.TAUNT:
                StartCoroutine(TauntState());
                break;
            case BASEUNIT_STATE.RETREAT:
                StartCoroutine(RetreatState());
                break;
            default:
                Debug.Log("StateKey Error! ");
                StartCoroutine(IdleState());
                break;
        }

    }

    protected virtual bool CommonState()
    {
        UnitData unitstat = gameObject.GetComponent<UnitStatus>().m_unitData;

        //State
        if (unitstat.iCurrentHP <= 0)
        {
            unitstat.m_CurrentUnitState = BASEUNIT_STATE.DEAD;
            return true;
        }
        else
        {
            if (unitstat.iCurrentMorale <= 0)
            {
                unitstat.m_CurrentUnitState = BASEUNIT_STATE.RETREAT;
                return true;
            }
        }

        return false;
    }


    void OnCollisionStay2D(Collision2D CollObj)
    {
        if (CollObj.gameObject.tag == "Player")
            m_ObjCloseAtkTarget = CollObj.gameObject;
    }

    void OnCollisionExit2D(Collision2D CollObj)
    {
        if (CollObj.gameObject.tag == "Player")
            m_ObjCloseAtkTarget = null;
    }
}
